package com.chensi.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chensi.annotation.CacheEvict;
import com.chensi.annotation.CacheMethod;
import com.chensi.annotation.Cacheable;
import com.chensi.dao.IUserMapper;
import com.chensi.model.User;
import com.chensi.service.IUserService;
@Service
public class UserService extends CommonService<User> implements IUserService
{
	@Autowired
	private IUserMapper userMapper;
	
	protected void init(){
		super.mapper=this.userMapper;
	}
	
	/**
	 * redis缓存格式统一：
	 * String==>string:xx
	 * List  ==>list:xx
	 * Set   ==>set:xx
	 * Map   ==>map:xx
	 * Object==>object:xx (注意object必须要被序列化)
	 */
	@Cacheable("string:name")
	@Override
	public String getRedisString()
	{
		String str="陈";
		return str;
	}
	
	@Cacheable("list:user")
	@Override
	public List<User> getRedisObjectList(){
		List<User> list=new ArrayList<User>();
		list.add(new User(1, "张三"));
		list.add(new User(2, "李四"));
		return list;
	}

	
	/**
	 * 参数写成包装类格式，防止与基本类型反射不一致
	 * method=pk表示 缓存key=key_{id}
	 */
	@Cacheable(value="object:user",method=CacheMethod.PK)
	@Override
	public User getRedisObject(Integer id)
	{
		User user=userMapper.get(id);
		return user;
	}

	/**
	 * 删除缓存，1：直接删除value 2：删除value_key(#u.id对应了参数u的id)
	 */
	@CacheEvict(value="object:user",key="#u.id")
	@Override
	public void update(User u)
	{
		userMapper.update(u);
	}
	
	@Override
	public void save(User u){
		userMapper.save(u);
	}

	/** #id对应了参数id */
	@CacheEvict(value="object:user",key="#id")
	@Override
	public void remove(Serializable id)
	{
		userMapper.remove(id);
	}

}
